package com.contract.spirit.ui.main.first

import android.view.View
import androidx.fragment.app.Fragment
import com.contract.commonlib.util.ext.setOnSingleClickListener
import com.contract.commonlib.base.BaseFragment
import com.contract.commonlib.http.ext.parseState
import com.contract.commonlib.refresh.IRefreshCallBack
import com.contract.commonlib.refresh.RefreshManager
import com.contract.commonlib.util.CommonUtils
import com.contract.commonlib.util.RecycleviewLayoutManger
import com.contract.spirit.R
import com.contract.spirit.bean.entity.BannerInfo
import com.contract.spirit.bean.entity.DialogBean
import com.contract.spirit.bean.response.MainPageResponse
import com.contract.spirit.databinding.FrgFristpageBinding
import com.contract.spirit.func.MyViewPagerAdapter
import com.contract.spirit.func.dialog.CommonRuleTipsDialog
import com.contract.spirit.manage.BindExchangeDialogManager
import com.contract.spirit.manage.CommonConfigManager
import com.contract.spirit.ui.invite.InviteActivity
import com.contract.spirit.ui.main.first.adapter.BannerImgAdapter
import com.contract.spirit.ui.main.first.adapter.ExchangePollAdapter
import com.contract.spirit.ui.main.first.adapter.FirstPageRankAdapter
import com.contract.spirit.ui.order.MyOrderActivity
import com.youth.banner.indicator.DrawableIndicator


/**
 * @author: witness
 * created: 2021/9/17
 * desc:
 */
class MainFirstPageFrg : BaseFragment<FrgFristpageBinding, MainFirstViewModel>(),
    IRefreshCallBack {
    override fun setContentView(): Int {
        return R.layout.frg_fristpage
    }

    override fun initData() {
        initExchangePollInfo()
    }

    override fun onReload() {
        super.onReload()
        mViewModel.mainPageResponse.value = null
        requestData()
    }

    override fun initListener() {
        mBinding.vgInvitePoll.setOnSingleClickListener {
            InviteActivity.launch(mContext)
        }
        mBinding.tvRule.setOnSingleClickListener {
            //拿奖规则
            showRuleDialog()
        }
    }

    private var ruleTipsDialog: CommonRuleTipsDialog? = null
    private fun showRuleDialog() {
        if (CommonUtils.isPopShow(ruleTipsDialog)) {
            return
        }
        val dialogBean = DialogBean()
        dialogBean.apply {
            isShowClose = true
            leftBtnText = ""
            rightBtnText = ""
            subTitle = CommonConfigManager.configInfoResponse.tipInfo.inviteTip
            title = "拿奖规则"
            ruleTipsDialog = CommonRuleTipsDialog.show(mContext, this)
        }
    }

    override fun initRefresh() {
        RefreshManager.getInstance()
            .initRefresh(mContext, null, false, mBinding.refreshLayout, this)
    }

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        mViewModel.mainPageResponse.observe(this, { it ->
            parseState(it, {
                mBinding.indexInfo = it
                bindRankInfo(it)
                exchangePollAdapter.setList(it.exchangeList)
            })
            endRefreshing(mBinding.refreshLayout)
        })
        mViewModel.bannerInfo.observe(this, { it ->
            parseState(it, {
                bindBannerInfo(it)
            })
        })
    }

    private val exchangePollAdapter by lazy {
        ExchangePollAdapter()
    }

    private fun initExchangePollInfo() {
        RecycleviewLayoutManger.setVerticalManager(mBinding.rvExchangePoll, mContext)
        mBinding.rvExchangePoll.adapter = exchangePollAdapter
        exchangePollAdapter.setOnItemClickListener { _, _, position ->
            val item = exchangePollAdapter.getItem(position)
            if (item.isHasBind()) {
                MyOrderActivity.launch(mContext, item.exchangeType)
            } else {
                BindExchangeDialogManager.showBindExchangeDialog(mContext, item)
            }
        }
        exchangePollAdapter.setEmptyView(R.layout.net_no_data_layout)
    }

    private fun bindBannerInfo(it: List<BannerInfo>) {
        mBinding.banner.apply {
            if (it.size < 0) {
                visibility = View.GONE
            } else {
                visibility = View.VISIBLE
                setAdapter(BannerImgAdapter(mContext, it))
                addBannerLifecycleObserver(mContext)
                indicator = DrawableIndicator(
                    mContext,
                    R.mipmap.banner_tab_normal,
                    R.mipmap.banner_tab_select
                )
                setBannerGalleryEffect(1, 25, 15, 0.85f)
            }
        }
    }

    private var frgList = mutableListOf<Fragment>()

    private fun bindRankInfo(mainPageResponse: MainPageResponse) {
        if (frgList.size <= 0) {
            val titles = arrayOf("邀请奖励", "开仓达人", "获赔达人")
            mBinding.apply {
                frgList.apply {
                    add(
                        FirstPageRankFrg.newsInstance(
                            FirstPageRankAdapter.INVITE,
                            mainPageResponse
                        )!!
                    )
                    add(
                        FirstPageRankFrg.newsInstance(
                            FirstPageRankAdapter.OPEN,
                            mainPageResponse
                        )!!
                    )
                    add(
                        FirstPageRankFrg.newsInstance(
                            FirstPageRankAdapter.COMPENSATED,
                            mainPageResponse
                        )!!
                    )
                }
                vpRank.adapter = MyViewPagerAdapter(childFragmentManager, frgList)
                rankTab.initViewPager(vpRank)
                rankTab.initTab(titles)
                vpRank.offscreenPageLimit = titles.size
            }
        } else {
            for (i in frgList.indices) {
                val rankFrg = frgList[i] as FirstPageRankFrg
                rankFrg.refreshRankInfo(mainPageResponse)
            }
        }
    }

    override fun requestData() {
        getIndexInfo()
        getBannerInfo()
    }

    private fun getIndexInfo() {
        mViewModel.getFirstIndexInfo()
    }

    private fun getBannerInfo() {
        mViewModel.getBannerList()
    }

    override fun onRefresh() {
        requestData()
    }

    override fun onResume() {
        super.onResume()
        requestData()
    }
}